package com.example.uCenter.user.controller;

import com.example.uCenter.common.Code;
import com.example.uCenter.common.Result;
import com.example.uCenter.user.entity.Information;
import com.example.uCenter.user.entity.Register;
import com.example.uCenter.user.entity.User;
import com.example.uCenter.user.service.IInformationService;
import com.example.uCenter.user.service.IUserService;
import com.example.uCenter.utils.SaltUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前端控制器
 *
 * @author yzh
 * @since 2022-08-29
 */
@CrossOrigin
@RestController
@RequestMapping("/UCenter")
public class RegisterController {
    @Autowired
    IUserService us;

    @Autowired
    IInformationService is;

    @PostMapping("/register")
    public Integer UserAnInformation(@RequestBody Register register) {
        List<User> user = us.selectAllByUser();
        List<Information> information = is.selectAllByInformation();
        Integer aa = 1;
        if (user != null && information != null) {
            boolean flag = false;
            for (int i = 0; i < user.size(); i++) {
                for (int j = 0; j < information.size(); j++) {
                    if (user.get(i).getUserName().equals(register.getUserName())
                            || information.get(j).getPhoneNum().equals(register.getPhone())
                            || information.get(j).getEmail().equals(register.getEmail())
                            || user.get(i).getIdCard().equals(register.getIdcard())) {
                        aa = 0;
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    break;
                }
            }
        }
        return aa;
    }

    /**
     * 注册
     * @param register
     * @return
     */
    @PostMapping("/addUser")
    public Result addUserAndInformation(@RequestBody Register register) {
        int uid = us.selectUserMaxId();

        User user = new User();
        Information information = new Information();
        //随机生成长度为5的字符串
        String salt = RandomStringUtils.randomAlphabetic(16);

        String pwdSalt = SaltUtil.AddSalt(salt, register.getPassword());

        user.setUserId(uid + 1);
        user.setUserName(register.getUserName());
        user.setPassword(pwdSalt);
        user.setSex(register.getSex());
        user.setIdCard(register.getIdcard());
        user.setSalt(salt);


        information.setUserId(uid + 1);
        information.setPhoneNum(register.getPhone());
        information.setEmail(register.getEmail());
        information.setCompony(register.getCompony());
        information.setPosition(register.getPosition());
        information.setGoodAt(register.getGoodat());
        information.setRealName(register.getRealName());
        information.setBirthday(register.getBirthday());
        information.setLocation(register.getLocation());


        boolean b = false;
        boolean b1 = false;

        b = us.RegisterByUserAdd(user);
        b1 = is.RegisterByInformationAdd(information);

        if (b && b1) {
            return new Result(true, Code.RegisterAdd_OK, "插入数据成功");
        } else {
            return new Result(false, Code.RegisterAdd_ERROR, "插入数据失败");
        }
    }
}
